﻿<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0,minimum-scale=1.0,maximum-scale=1.0" />
    <meta name="author" content="火星科技 http://mars2d.cn " />
    <meta name="apple-touch-fullscreen" content="yes" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="x5-fullscreen" content="true" />
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
    <link rel="shortcut icon" type="image/x-icon" href="https://cdn.jsdelivr.net/gh/muyao1987/cdn/marsgis.cn/favicon/favicon.ico" />

    <meta name="keywords" content="火星科技,leaflet,mars2d,GIS,marsgis,二维,地图,地球,开发,框架,系统,示例,资料,模型,离线,外包,合肥,安徽,中国" />
    <meta
      name="description"
      content="火星科技 合肥火星 合肥火星科技 合肥火星科技有限公司 leaflet leaflet框架 leaflet开发 leaflet leaflet开发 leaflet框架 二维 地图 模型  gis marsgis 地图离线 地图开发 地图框架 地图外包 框架 开发 外包  地图离线 二维地图 二维地图 全景漫游 地理信息系统 云GIS 二维GIS GIS平台 WebGIS"
    />

    <title>Mars2D地图 |二维地图| 火星科技|合肥火星科技|合肥火星科技有限公</title>

    <script
      type="text/javascript"
      src="../lib/include-lib.js"
      libpath="../lib/"
      include="jquery,font-awesome,bootstrap,layer,haoutil,mars2d,turf"
    ></script>

    <link href="css/style.css" rel="stylesheet" />
  </head>
  <body>
    <div id="mars2dContainer" class="mars2d-container"></div>

    <!-- 面板 -->
    <div class="infoview">
      <input type="button" class="btn btn-primary" value="开始" onclick="graphicLayer.snakeIn();" />
      <input type="button" class="btn btn-primary" value="停止" onclick="graphicLayer.snakeOut();" />

      <input type="button" class="btn btn-primary" value="暂停" onclick="graphicLayer.snakePause();" />
      <input type="button" class="btn btn-primary" value="继续" onclick="graphicLayer.snakeContinue();" />
    </div>

    <script src="./js/common.js"></script>
    <script type="text/javascript">
      "use script"; //开发环境建议开启严格模式

      var map;
      let graphicLayer;

      function initMap(options) {
        //合并属性参数，可覆盖config.json中的对应配置
        let mapOptions = mars2d.Util.merge(options, {
          zoom: 14,
          center: { lng: 117.189154, lat: 31.844478 },
        });

        //创建地图
        map = new mars2d.Map("mars2dContainer", mapOptions);

        var arrPath = [
          { name: "汇展园社区", jd: 117.144985, wd: 31.851898 },
          { name: "丰北社区", jd: 117.186699, wd: 31.852336 },
          { name: "桂林路社区", jd: 117.214336, wd: 31.857147 },

          { name: "汇展园社区", jd: 117.212963, wd: 31.840816 },
          { name: "鲤鱼山路社区", jd: 117.212963, wd: 31.833087 },
          { name: "瑞昌街明山社区", jd: 117.225838, wd: 31.832795 },
          { name: "三工村上二队", jd: 117.225838, wd: 31.829003 },
          { name: "三工村下二队", jd: 117.231159, wd: 31.82842 },
          { name: "唐山路社区", jd: 117.230816, wd: 31.851461 },
          { name: "桂林路社区", jd: 117.214336, wd: 31.857147 },
          { name: "丰北社区", jd: 117.186699, wd: 31.852336 },
          { name: "汇展园社区", jd: 117.144985, wd: 31.851898 },
          { name: "丰北社区", jd: 117.186699, wd: 31.852336 },
          { name: "桂林路社区", jd: 117.214336, wd: 31.857147 },
        ];

        //创建矢量数据图层
        graphicLayer = new mars2d.layer.GraphicLayer();
        map.addLayer(graphicLayer);

        //记录历史值，判断是否重叠路线
        var arrPathKey = {};

        for (var i = 0; i < arrPath.length; i++) {
          var endItem = arrPath[i];
          var endMpt = [endItem.wd, endItem.jd];

          if (i != 0) {
            var startItem = arrPath[i - 1];
            var startMpt = [startItem.wd, startItem.jd];

            //计算重叠次数
            var _key = startItem.name + endItem.name;
            if (arrPathKey.hasOwnProperty(_key)) {
              arrPathKey[_key] = arrPathKey[_key] + 1;
            } else {
              arrPathKey[_key] = 0;

              _key = endItem.name + startItem.name;
              if (!arrPathKey.hasOwnProperty(_key)) {
                arrPathKey[_key] = 0;
              }
            }

            //var latlngs = [startMpt, endMpt]; //不做任何处理时
            var latlngs = getBezierPoints(startMpt, endMpt, arrPathKey[_key]);

            var graphic = new mars2d.graphic.Polyline({
              latlngs: latlngs,
              style: {
                color: "#0000ff",
                width: 3,
              },
            });
            graphicLayer.addGraphic(graphic);
          }

          let num = i + 1;
          var marker = new mars2d.graphic.DivGraphic({
            latlng: endMpt,
            style: {
              html: `<div style="width: 50px; height: 50px;">
                      <img src="img/marker/b1.png" />
                    </div>
                    <div style="position: absolute;left:${num < 10 ? "22" : "18"}px;top: 3px; ">${num}</div>`,
              horizontalOrigin: mars2d.HorizontalOrigin.CENTER,
              verticalOrigin: mars2d.VerticalOrigin.BOTTOM,
            },
          });
          graphicLayer.addGraphic(marker);

          marker.bindPopup("<b>" + endItem.name + "</b>");
        }

        // map.fitBounds(graphicLayer.getBounds());

        //开始运动
        graphicLayer.snakeIn();
      }

      //计算贝塞尔曲线
      function getBezierPoints(startMpt, endMpt, count) {
        if (count == 0) {
          return [startMpt, endMpt];
        }

        startMpt = L.latLng(startMpt);
        endMpt = L.latLng(endMpt);

        var start = turf.point([startMpt.lng, startMpt.lat]);
        var end = turf.point([endMpt.lng, endMpt.lat]);
        var midpoint = turf.midpoint(start, end);

        var distance = startMpt.distanceTo(endMpt) / (1000 * 20);
        distance = distance * count;

        var bearing = turf.bearing(start, end) + 90;
        var destination = turf.destination(midpoint, distance, bearing, { units: "kilometers" });

        var line = turf.lineString([start.geometry.coordinates, destination.geometry.coordinates, end.geometry.coordinates]);
        var greatCircle = turf.bezier(line);

        var latlngs = L.GeoJSON.coordsToLatLngs(greatCircle.geometry.coordinates);
        return latlngs;
      }
    </script>
  </body>
</html>
